package com.wyw.hemerocallis.partiition;

import com.alibaba.fastjson.JSON;
import com.wyw.hemerocallis.vo.WywMessage;
import lombok.extern.slf4j.Slf4j;
import org.springframework.cloud.stream.binder.PartitionKeyExtractorStrategy;
import org.springframework.messaging.Message;
import org.springframework.stereotype.Component;

/**
 * 自定义从 Message 中提取 partition key 的策略
 *
 * @author Mr Wu    yewen.wu.china@gmail.com
 * <p>
 * Update History:
 * Author        Time            Content
 */
@Slf4j
@Component
public class WywPartitionKeyExtractorStrategy implements PartitionKeyExtractorStrategy {
    @Override
    public Object extractKey(Message<?> message) {

        WywMessage wywMessage = JSON.parseObject(message.toString(), WywMessage.class);

        //自定义提取key，这里可以对key进行处理 加密啥的
        String key = wywMessage.getProjectName();

        log.info("Spring Cloud Stream Wyw Partition Key: [{}]", key);

        return key;
    }
}
